﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class IntQuestion
    {
        public void IntQuestionFunc()
        {
            //SerachForAvg();
            //RemoveDuplicateChar();
            Move_m_Chars();
        }

        //去除数组中的最大最小值，剩余的求AVG，只能遍历一次
        private void SerachForAvg()
        {            
            int[] arr = { 10, 20, 40, 90, 100};
            int max = arr[0];
            int min = arr[0];
            int sum = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                if (max < arr[i])
                    max = arr[i];

                if (min > arr[i])
                    min = arr[i];

                sum += arr[i];
            }
            double avg = (sum - max - min) / (arr.Length - 2);
            Console.WriteLine("Max:" + max + " Min:" + min + " AVG:" + avg);
        }

        //删除字符串中相邻字符重复的部分 abbbcccddabbbc => abcdabc
        private void RemoveDuplicateChar()
        {             
            string oriStr = "abbbcccddabbbc";

            char[] oriArr = oriStr.ToCharArray();
            char temp = oriArr[0];
            StringBuilder sb=new StringBuilder();
            sb.Append(temp);
            for (int i = 1; i < oriArr.Length ; i++)
            {
                //用第一个字符作为起始标识，遍历跟后面的字符相比，如果不等说明是不重复字符，添加到列表中
                if(temp != oriArr[i])
                {
                    temp = oriArr[i];
                    sb.Append(temp);
                }                    
            }
            Console.WriteLine(sb.ToString());
        }

        //将长度为n的字符串向左循环移动m位(常数空间,不能用新变量去截取拼接)  eg.移动3位 abcdefg => defgabc
        private void Move_m_Chars()
        {
            int m = 3;
            string oriStr = "abcdefg";
            char[] a = oriStr.ToCharArray();
            
            //把数组分成m和n-m两部分，分别逆序，然后整个数组逆序
            for (int i = 0; i < m; i++)
            {
                if (i <= m - 1 - i)
                {
                    char temp = a[i];
                    a[i] = a[m - 1 - i];
                    a[m - 1 - i] = temp;
                }
                else
                    break;
            }

            for (int i = m; i < a.Length; i++)
            {
                if (i <= a.Length + m - 1 - i)
                {
                    char temp = a[i];
                    a[i] = a[a.Length + m - 1 - i];
                    a[a.Length + m - 1 - i] = temp;
                }
                else
                    break;
            }

            Array.Reverse(a);

            string t = new string(a);
            Console.WriteLine(t);

        }

    }
}
